<template>
	<view class="uni-container">
		<view class="userinfo">
			<view class="nickname-wrapper">
				<view class="nickname-left">
					<text class="nickname-label">头像：</text>
				</view>
				<view>
					<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" slot="right">
						<image class="avatar" :src="avatar"></image>
					</button>
				</view>
			</view>
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">昵称：</text>
				</view>
				<view>
					<input type="nickname" class="nickname-input" v-model="nickname" @blur="userNameInput" placeholder="请输入昵称" />
				</view>
			</view>
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">openid：</text>
				</view>
				<view class="nickname-right">
					<input type="text" class="nickname-input" v-model="openid" placeholder="用户openid" />
					<button type="primary" size="mini" plain="true" @click="wxLogin">获取用户openid</button>
				</view>
			</view>
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">手机号：</text>
				</view>
				<view class="nickname-right">
					<input type="text" class="nickname-input" v-model="phone" placeholder="用户手机号" />
					<button type="primary" size="mini" plain="true" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">获取用户手机号</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//url: 'http://www.tp6.com/index.php', 	//后台请求域名，需要修改你自己的本地或者线上域名
				url: 'http://www.tp8.com/index.php', //后台请求域名，需要修改你自己的本地或者线上域名
				openid: '',
				phone: '',
				avatar: '',
				nickname: '',
			};
		},
		onLoad(options) {
			//console.log(123);
		},
		onShow() {
			//console.log(456);
		},
		methods: {
			wxLogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						//console.log('登录成功，code为：' + res.code);
						// 获取临时登录凭证code
						uni.request({
							url: this.url + '/wechat/getOpenid',
							method: 'POST',
							data: {
								code: res.code
							},
							header: {},
							success: (res) => {
								//console.log(res.data);
								this.openid = res.data.data;
							}
						});
					},
					fail: (err) => {
						console.error('登录失败：', err);
					}
				});
			},
			onGetPhoneNumber(e) {
				//console.log("用户信息：", e.detail);
				if (e.detail.code) {
					uni.request({
						url: this.url + '/wechat/getPhoneNumber',
						method: 'POST',
						data: {
							phone_code: e.detail.code
						},
						header: {},
						success: (res) => {
							//console.log(res.data);
							this.phone = res.data.data;
						}
					});
				} else {
					console.log("用户信息：", e.detail);
				}
			},
			//获取微信头像
			onChooseAvatar(e) {
				this.avatar = e.detail.avatarUrl;
			},
			//获取昵称输入内容
			userNameInput(e) {
				this.nickname = e.detail.value
			},
		}
	};
</script>

<style lang="scss">
	.uni-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		padding: 20rpx;
	}

	.userinfo {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #aaa;
		width: 90%;
	}

	.userinfo-avatar {
		overflow: hidden;
		width: 128rpx;
		height: 128rpx;
		margin: 20rpx;
		border-radius: 50%;
	}

	.avatar-wrapper {
		padding: 0;
		width: 56px !important;
		border-radius: 8px;
		margin-top: 60rpx;
		margin-bottom: 60rpx;
	}

	.avatar {
		display: block;
		width: 56px;
		height: 56px;
	}

	.nickname-wrapper {
		display: flex;
		width: 100%;
		padding: 16px;
		box-sizing: border-box;
		color: black;
		margin-bottom: 30rpx;
	}
	
	.border {
		border: .5px solid rgba(0, 0, 0, 0.1);
	}

	.nickname-left {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.nickname-right input {
		height: 60rpx;
		border: .5px solid rgba(0, 0, 0, 0.1);
		margin-bottom: 30rpx;
	}

	.nickname-label {
		width: 80px;
	}

	.nickname-input {
		flex: 1;
		font-size: 24rpx;
		padding: 0 10rpx;
		width: 380rpx;
	}
</style>